package com.example.dilidili_ting.utils;

import com.google.gson.JsonObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkhttpUtilsTing {
    public static MediaType JSON= MediaType.parse("application/json;charset=utf-8");

    //get异步请求
    public static void isGET(String action, boolean IP, Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request.Builder builder = new Request.Builder();
//        String ip = IP?"http://yanling.gz2vip.idcfengye.com/":"http://106.55.142.207:81/";
//        String ip = IP?"http://192.168.157.29:8989/":"http://192.168.157.29:8989/";
        String ip = IP?"http://192.168.135.167:8989/":"http://192.168.135.167:8989/";
//        String ip = IP?"http://192.168.33.190:8989/":"http://192.168.33.190:8989/";
        String url = ip+action;
        Request request=builder.get().url(url).build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }
    //post异步请求
    public static void isPOST(String action, boolean IP,  JsonObject jsonObject, Callback callback){
        OkHttpClient client=new OkHttpClient();
        Request.Builder builder = new Request.Builder();
//        String ip = IP?"http://yanling.gz2vip.idcfengye.com/":"http://106.55.142.207:81/";
//        String ip = IP?"http://192.168.157.29:8989/":"http://192.168.157.29:8989/";
        String ip = IP?"http://192.168.135.167:8989/":"http://192.168.135.167:8989/";
//        String ip = IP?"http://192.168.33.190:8989/":"http://192.168.33.190:8989/";
        String url = ip+action;
        RequestBody body= RequestBody.create(JSON,String.valueOf(jsonObject));
        Request request=builder.post(body).url(url).build();
        Call call = client.newCall(request);
        call.enqueue(callback);
    }

}
